﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class ListProduct : Form
    {
        TblProductsDAO ProductsDAO = new TblProductsDAO(Global.CONNECTION_STRING);

        public ListProduct()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewProduct newProduct = new NewProduct();
            newProduct.Show();
            this.Hide();
        }

        private void ListProduct_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            LoadProduct_Sell();
            LoadProduct_Part();
        }

        // load product sell
        private void LoadProduct_Sell()
        {
            grdSell.DataSource = ProductsDAO.GetProductsList(2);
        }

        // load product part
        private void LoadProduct_Part()
        {
            grdPart.DataSource = ProductsDAO.GetProductsList(1);
        }

        private void btnDeleteSell_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (MessageBox.Show("Do you want to delete this Product ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    string idProduct = grdSell.CurrentRow.Cells["ProductID"].Value.ToString();
                    TblProducts product = new TblProducts();
                    product.ProductID = Global.ConvertToInt(idProduct);
                    ProductsDAO.DeleteProduct(Global.ConvertToInt(idProduct));
                    flag = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (flag)
            {
                LoadProduct_Sell();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (MessageBox.Show("Do you want to delete this Product ?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    string idProduct = grdPart.CurrentRow.Cells["IDProduct"].Value.ToString();
                    TblProducts product = new TblProducts();
                    product.ProductID = Global.ConvertToInt(idProduct);
                    ProductsDAO.DeleteProduct(Global.ConvertToInt(idProduct));
                    flag = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (flag)
            {
                LoadProduct_Part();
            }
        }

        private void btnDetailSell_Click(object sender, EventArgs e)
        {
            string idProduct = grdSell.CurrentRow.Cells["ProductID"].Value.ToString();
            NewProduct newProduct = new NewProduct();
            newProduct.Sender(idProduct); //Gọi delegate
            this.Hide();
            newProduct.Show();    
        }

        private void btnDetailPart_Click(object sender, EventArgs e)
        {
            string idProduct = grdPart.CurrentRow.Cells["IDProduct"].Value.ToString();
            NewProduct newProduct = new NewProduct();
            newProduct.Sender(idProduct); //Gọi delegate
            this.Hide();
            newProduct.Show();  
        }
    }
}
